layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , form = layui.form;

    //分类管理
    table.render({
        elem: '#LAY-app-content-categories'
        , url: '/forum_category'
        , cols: [[
            {type: 'numbers', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'name', title: '分类名', minWidth: 100}
            , {field: 'sort', title: '排序'}
            , {field: 'status', title: '状态', templet: '#buttonTpl', minWidth: 80, align: 'center'}
            , {field: 'created_at', title: '创建时间'}
            , {field: 'updated_at', title: '更新时间'}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#layuiadmin-app-cont-categoriesbar'}
        ]]
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-app-content-categories)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此分类？', function (index) {
                admin.req({
                    url: '/forum_category/' + data.id,
                    type: "delete",
                    success: function (res) {
                        if (!res.code) {
                            obj.del();
                            layer.msg(res.message, {icon: 6});
                        } else {
                            layer.msg(res.message, {icon: 5});
                        }
                        layer.close(index);
                    }
                });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑分类'
                , area: ['500px', '450px']
                , id: 'LAY-popup-content-tags'
                , success: function (layero, index) {
                    view(this.id).render('app/forum/categoryform', data).done(function () {
                        form.render(null, 'layuiadmin-form-categoriesbar');

                        //监听提交
                        form.on('submit(layuiadmin-app-categories-submit)', function (forms) {
                            admin.req({
                                url: '/forum_category/' + data.id,
                                data: forms.field,
                                type: "put",
                                success: function (res) {
                                    if (!res.code) {
                                        layer.msg(res.message, {icon: 6});
                                        table.reload('datatable');
                                    } else {
                                        layer.msg(res.message, {icon: 5});
                                    }
                                }
                            });
                            layer.close(index);
                        });
                    });
                }
            });
        }
    });

    //帖子管理
    table.render({
        elem: '#LAY-app-forum-list'
        , url: '/forum'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'title', title: '发帖主题'}
            , {field: 'nickname', title: '发帖人', templet: '<div>{{ d.user.nickname }}</div>'}
            , {field: 'avatar', title: '头像', width: 100, templet: '#imgTpl'}
            , {field: 'created_at', title: '创建时间'}
            , {field: 'stick', title: '置顶', templet: '#buttonTpl', width: 90, align: 'center'}
            , {title: '操作', width: 220, align: 'center', fixed: 'right', toolbar: '#table-forum-list'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //工具条
    table.on('tool(LAY-app-forum-list)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此条帖子？', function (index) {
                admin.req({
                    url: '/forum/' + data.id,
                    type: "delete",
                    success: function (res) {
                        if (!res.code) {
                            obj.del();
                            layer.msg(res.message, {icon: 6});
                        } else {
                            layer.msg(res.message, {icon: 5});
                        }
                        layer.close(index);
                    }
                });
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2
                ,title: '编辑帖子'
                ,content: '/forum/'+data.id+'/edit'
                ,area: ['1000px', '600px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero) {
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-app-forum-list-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);

                    //ue.ready(function () {
                    //    //设置内容
                    //    ue.setContent(data.content);
                    //});

                    //监听提交
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(res){

                        //获取内容
                        //res.field.content = ue.getContent();
                        console.log(res);

                        $.ajax({
                            url: "/forum/" + data.id,
                            data: res.field,
                            type: "put",
                            dataType: "json",
                            success:function(data){
                                if(data.code===0){
                                    layer.msg(data.msg, {icon: 6});
                                    obj.update({
                                        label: field.label
                                        , title: field.title
                                        , author: field.author
                                        , status: field.status
                                    }); //数据更新
                                    //table.reload('LAY-user-front-submit'); //数据刷新
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(data.msg, {icon: 5});
                                }
                            },
                            error:function(data){
                                console.log(data);

                                if (data.status === 422) {
                                    let response = $.parseJSON(data.responseText);
                                    $.each(response.errors, function (key, val) {
                                        $("#" + "ajax_" + key + "_error").text(val[0]);
                                    });
                                }
                            }
                        });
                    });

                    submit.trigger('click');
                }
            })
        }
    });

    //回帖管理
    table.render({
        elem: '#LAY-app-forumreply-list'
        , url: '/forum/' + layui.router().search.forum_id + '/comment'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'nickname', title: '回帖人', templet: '<div>{{ d.user.nickname }}</div>'}
            , {field: 'avatar', title: '头像', width: 100, templet: '#imgTpl'}
            , {field: 'commentable_id', title: '回帖ID', sort: true}
            , {field: 'content', title: '回帖内容', width: 200}
            , {field: 'created_at', title: '回帖时间', sort: true}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-forum-replys'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //工具条
    table.on('tool(LAY-app-forumreply-list)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此条评论？', function (index) {
                admin.req({
                    url: '/forum/' + data.id,
                    type: "delete",
                    success: function (res) {
                        if (!res.code) {
                            obj.del();
                            layer.msg(res.message, {icon: 6});
                        } else {
                            layer.msg(res.message, {icon: 5});
                        }
                        layer.close(index);
                    }
                });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑回帖'
                , area: ['550px', '400px']
                , id: 'LAY-popup-forum-edit'
                , resize: false
                , success: function (layero, index) {
                    view(this.id).render('app/forum/replysform', data).done(function () {
                        form.render(null, 'layuiadmin-app-forum-reply');

                        //提交
                        form.on('submit(layuiadmin-app-forumreply-submit)', function (data) {
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            //$.ajax({});
                            layui.table.reload('LAY-app-forumreply-list'); //重载表格
                            layer.close(index); //执行关闭
                        });
                    });
                }
            });
        }
    });

    exports('forum', {})
});
